﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace DataManager.DAL
{
    public struct Lending
    {
        int m_LendingId;
        int m_MovieId;
        int m_CustomerId;
        int m_EmployeeId;
        DateTime m_startDate;
        int m_Duration;
        int m_Price;
        DateTime m_BackDate;
        int m_Penalty;

        public int LendingId
        {
            get { return m_LendingId; }
            set { m_LendingId = value; }
        }

        public int MovieId
        {
            get { return m_MovieId; }
            set { m_MovieId = value; }
        }

        public int CustomerId
        {
            get { return m_CustomerId; }
            set { m_CustomerId = value; }
        }

        public int EmployeeId
        {
            get { return m_EmployeeId; }
            set { m_EmployeeId = value; }
        }

        public DateTime StartDate
        {
            get { return m_startDate; }
            set { m_startDate = value; }
        }

        public int Duration
        {
            get { return m_Duration; }
            set { m_Duration = value; }
        }

        public int Price
        {
            get { return m_Price; }
            set { m_Price = value; }
        }

        public DateTime BackDate
        {
            get { return m_BackDate; }
            set { m_BackDate = value; }
        }

        public int Penalty
        {
            get { return m_Penalty; }
            set { m_Penalty = value; }
        }
    }

    class Lendings:DAL
    {
        /// <summary>
        /// Visszateriti azokat a kolcsonzeseket ahol a vasarlonev a parameter es meg nincs visszaadva a dvd
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Lending> GetNotRestoredLendingsList(string name)
        {
            string query = "SELECT KolcsonzesId, FilmId, KolcsDatum, Idotartam "+
                "FROM Kolcsonzesek, Vasarlok " + 
                "WHERE Kolcsonzesek.VasarloId = Vasarlok.VasarloId " +
                "AND Vasarlok.Nev LIKE '%" + name +
                "%' AND Kolcsonzesek.VisszaAdva IS NULL";
            string error = string.Empty;
            SqlDataReader rdr = ExecuteReader(query, ref error);
            List<Lending> lendingList = new List<Lending>();
            if (error == "OK")
            {
                while (rdr.Read())
                {
                    Lending item = new Lending();
                    item.LendingId = Convert.ToInt32(rdr["KolcsonzesId"]);
                    item.MovieId = Convert.ToInt32(rdr["FilmId"]);
                    item.StartDate = Convert.ToDateTime(rdr["KolcsDatum"]);
                    item.Duration = Convert.ToInt32(rdr["Idotartam"]);
                    item.Price = 0;
                    item.BackDate = DateTime.MinValue;
                    item.Penalty = 0;
                    lendingList.Add(item);
                }
            }
            CloseDataReader(rdr);
            return lendingList;
        }
       
        /// <summary>
        /// Beszur egy uj kolcsonzest tarolt eljaras hasznalataval.
        /// </summary>
        /// <param name="FilmId"></param>
        /// <param name="VasarloId"></param>
        /// <param name="EladoId"></param>
        /// <param name="KolcsDatum"></param>
        /// <param name="Idotartam"></param>
        /// <param name="Ertek"></param>
        /// <returns></returns>
        public string InsertLending(int FilmId,int VasarloId,int EladoId,DateTime KolcsDatum,int Idotartam,int Ertek)
        {
            string[] parameterNames = new string[6], parameterValues = new string[6];
            parameterNames[0] = "@FilmId"; parameterValues[0] = FilmId.ToString();
            parameterNames[1] = "@VasarloId"; parameterValues[1] = VasarloId.ToString();
            parameterNames[2] = "@EladoId"; parameterValues[2] = EladoId.ToString();
            parameterNames[3] = "@KolcsDatum"; parameterValues[3] = KolcsDatum.ToString();
            parameterNames[4] = "@Idotartam"; parameterValues[4] = Idotartam.ToString();
            parameterNames[5] = "@Ertek"; parameterValues[5] = Ertek.ToString();

            return ExecuteStoredProcedureNonQuery("ujKolcsonzes", parameterNames, parameterValues);
        }

        /// <summary>
        /// Modositja a kolcsonzest, hogy az legyen visszaadva, tarolt eljaras segitsegevel.
        /// </summary>
        /// <param name="lendingId"></param>
        /// <param name="movieId"></param>
        public void RestoreLending(int lendingId, int movieId)
        {
            string[] parameterNames = new string[2], parameterValues = new string[2];
            parameterNames[0] = "@KolcsonzesId"; parameterValues[0] = lendingId.ToString();
            parameterNames[1] = "@FilmId"; parameterValues[1] = movieId.ToString();
            ExecuteStoredProcedureNonQuery("Visszaad", parameterNames, parameterValues);
        }
    }
}
